import {_decorator, Component, Label, resources, Sprite, SpriteFrame} from "cc";
import {MessageFixedType, MessageType} from "db://assets/script/game/constants/message";
import {propsConfigsLoader} from "db://assets/script/configs/props-configs-loader";
import {serverTimeHelper} from "db://assets/script/server-time-helper";
import {userInfo} from "db://assets/script/user-info";
import {cityUtils} from "db://assets/script/utils/city-utils";
import Long from "long";

const {ccclass, property} = _decorator;

@ccclass('MessageItem')
export class MessageItem extends Component {

    @property(Sprite)
    icon!: Sprite
    @property(Label)
    messageText!: Label
    messageType!: MessageType

    private fixedType = MessageFixedType.NOT_FIXED
    private _timeout: number = -1

    start() {

    }

    initNotFixed(messageType: MessageType, message: string, extra: Map<string, string>) {
        this.fixedType = MessageFixedType.NOT_FIXED;
        this._initContent(messageType, message, extra);
    }

    initFixed(messageType: MessageType, message: string, extra: Map<string, string>) {
        this.fixedType = MessageFixedType.ALWAYS;
        this._initContent(messageType, message, extra);
    }

    initTimedFixed(messageType: MessageType, message: string, timeout: number, extra: Map<string, string>) {
        this.fixedType = MessageFixedType.TIMED;
        this._timeout = timeout;
        this._initContent(messageType, message, extra);
    }


    private _initContent(messageType: MessageType, message: string, extra: Map<string, string>) {
        this.messageType = messageType;
        switch (messageType) {
            case MessageType.BeginBattle:
                this.icon.spriteFrame = resources.get('/textures/ui-icon/attack-message-icon/spriteFrame', SpriteFrame);
                this.messageText.string = message
                break;
            case MessageType.BattleComplete:
                this.icon.spriteFrame = resources.get('/textures/ui-icon/battle-complete-message-icon/spriteFrame', SpriteFrame);
                this.messageText.string = message
                break;
            case MessageType.Death:
                this.icon.spriteFrame = resources.get('/textures/ui-icon/death-message-icon/spriteFrame', SpriteFrame);
                this.messageText.string = message
                break;
            case MessageType.Explore:
                const propId = Number(extra.get('propId'))
                this.icon.spriteFrame = propsConfigsLoader.findPropIcon(propId)
                this.messageText.string = message
                break;
            case MessageType.FishFryCome:
                this.icon.spriteFrame = resources.get('/textures/ui-icon/fish-fry-come-message-icon/spriteFrame', SpriteFrame);
                this.messageText.string = message
                break;
            case MessageType.BuildingBuildComplete:
                const buildingId1 = extra.get('buildingId')!
                this.icon.spriteFrame = cityUtils.calcBuildingIcon(Long.fromString(buildingId1))
                this.messageText.string = message
                break;
            case MessageType.BuildingUpgradeComplete:
                const buildingId2 = extra.get('buildingId')!
                this.icon.spriteFrame = cityUtils.calcBuildingIcon(Long.fromString(buildingId2))
                this.messageText.string = message
                break;
            case MessageType.BuildingRemoveComplete:
                const buildingId3 = extra.get('buildingId')!
                this.icon.spriteFrame = cityUtils.calcBuildingIcon(Long.fromString(buildingId3))
                this.messageText.string = message
                break;
        }
    }

    checkFixed() {
        if (this.fixedType === MessageFixedType.NOT_FIXED) {
            return false;
        } else if (this.fixedType === MessageFixedType.ALWAYS) {
            return true;
        } else {
            const nowMs = serverTimeHelper.loadServerTime()
            return nowMs < this._timeout
        }
    }

}